home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / bin / os-prober < prev    next >
Encoding:
Text File  |  2009-03-16  |  3.5 KB  |  142 lines

  1. #!/bin/sh
  2. set -e
  3.  
  4. . /usr/share/os-prober/common.sh
  5.  
  6. require_tmpdir
  7.  
  8. log_output () {
  9.     if type log-output >/dev/null 2>&1; then
  10.         log-output -t os-prober --pass-stdout $@
  11.     else
  12.         $@
  13.     fi
  14. }
  15.  
  16. on_sataraid () {
  17.     type dmraid >/dev/null 2>&1 || return 1
  18.     local parent=${1%/*}
  19.     local device=/dev/${parent##*/}
  20.     if dmraid -r -c | grep -q $device; then
  21.         return 0
  22.     fi
  23.     return 1
  24. }
  25.  
  26. partitions () {
  27.     # Exclude partitions that have whole_disk sysfs attribute set.
  28.     if [ -d /sys/block ]; then
  29.         # Exclude partitions on physical disks that are part of a
  30.         # Serial ATA RAID disk.
  31.         for part in /sys/block/*/*[0-9]; do
  32.             if [ -f "$part/start" ] && \
  33.                [ ! -f "$part/whole_disk" ] && ! on_sataraid $part; then
  34.                 name="$(echo "${part##*/}" | sed 's,[!.],/,g')"
  35.                 if [ -e "/dev/$name" ]; then
  36.                     echo "/dev/$name"
  37.                 fi
  38.             fi
  39.         done
  40.  
  41.         # Add Serial ATA RAID devices
  42.         if type dmraid >/dev/null 2>&1 && \
  43.            dmraid -s -c >/dev/null 2>&1; then
  44.             for raidset in $(dmraid -sa -c); do
  45.                 for part in /dev/mapper/$raidset*[0-9]; do
  46.                     echo "$part"
  47.                 done
  48.             done
  49.         fi
  50.     else
  51.         echo "Cannot find list of partitions!" >&2
  52.         exit 1
  53.     fi
  54.  
  55.     # Also detect OSes on LVM volumes (assumes LVM is active)
  56.     if type lvs >/dev/null 2>&1; then
  57.         echo "$(log_output lvs --noheadings --separator : -o vg_name,lv_name |
  58.             sed "s|-|--|g;s|^[[:space:]]*\(.*\):\(.*\)$|/dev/mapper/\1-\2|")"
  59.     fi
  60. }
  61.  
  62. parse_proc_mdstat () {
  63.     if type udevadm >/dev/null 2>&1; then
  64.         udevinfo () {
  65.             udevadm info "$@"
  66.         }
  67.     fi
  68.     while read line; do
  69.         for word in $line; do
  70.             dev="${word%%[*}"
  71.             # TODO: factor this out to something in di-utils if
  72.             # it's needed elsewhere
  73.             if [ -d /sys/block ] && type udevinfo >/dev/null 2>&1; then
  74.                 if ! udevinfo -q path -n "/dev/$dev" 2>/dev/null | \
  75.                      grep -q '/.*/.*/'; then
  76.                     continue
  77.                 fi
  78.             elif ! echo "$dev" | grep -q "/part"; then
  79.                 continue
  80.             fi
  81.             raidpart="/dev/$dev"
  82.             echo "$(mapdevfs $raidpart)"
  83.         done
  84.     done
  85. }
  86.  
  87. # Needed for idempotency
  88. rm -f /var/lib/os-prober/labels
  89.  
  90. for prog in /usr/lib/os-probes/init/*; do
  91.     if [ -x $prog ] && [ -f $prog ]; then
  92.         $prog || true
  93.     fi
  94. done
  95.  
  96. # We need to properly canonicalize partitions with mount points and partitions
  97. # used in RAID
  98. grep "^/dev/" /proc/mounts | parse_proc_mounts >"$OS_PROBER_TMP/mounted-map" || true
  99. : >"$OS_PROBER_TMP/raided-map"
  100. if [ -f /proc/mdstat ] ; then
  101.     grep "^md" /proc/mdstat | parse_proc_mdstat >"$OS_PROBER_TMP/raided-map" || true
  102. fi
  103.  
  104. for partition in $(partitions); do
  105.     if ! mapped=$(mapdevfs $partition); then
  106.         log "Device '$partition' does not exist; skipping"
  107.         continue
  108.     fi
  109.  
  110.     # Skip partitions used in software RAID arrays
  111.     if grep -q "^$mapped" "$OS_PROBER_TMP/raided-map" ; then
  112.         debug "$partition: part of software raid array"
  113.         continue
  114.     fi
  115.  
  116.     if ! grep -q "^$mapped " "$OS_PROBER_TMP/mounted-map" ; then
  117.         for test in /usr/lib/os-probes/*; do
  118.             if [ -f $test ] && [ -x $test ]; then
  119.                 debug "running $test on $partition"
  120.                 if $test $partition; then
  121.                     debug "os detected by $test"
  122.                        break
  123.                 fi
  124.             fi
  125.         done
  126.     else
  127.         mpoint=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 2)
  128.         if [ "$mpoint" != "/target/boot" ] && [ "$mpoint" != "/target" ] && [ "$mpoint" != "/" ]; then
  129.             type=$(grep "^$mapped " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 3)
  130.             for test in /usr/lib/os-probes/mounted/*; do
  131.                 if [ -f $test ] && [ -x $test ]; then
  132.                     debug "running $test on mounted $partition"
  133.                     if $test $partition $mpoint $type; then
  134.                         debug "os detected by $test"
  135.                         break
  136.                     fi
  137.                 fi
  138.             done
  139.         fi
  140.     fi
  141. done
  142.